package com.whut.monitor.service.impl;

import com.whut.monitor.dao.IBaseDao;
import com.whut.monitor.model.EquipmentEntity;
import com.whut.monitor.model.PatternEntity;
import com.whut.monitor.service.IPatternService;
import com.whut.monitor.vo.PageFilter;
import com.whut.monitor.vo.PatternVo;
import com.whut.monitor.vo.Tree;
import org.springframework.beans.BeanUtils;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;

import java.util.ArrayList;
import java.util.HashMap;
import java.util.List;
import java.util.Map;

/**
 * Created by CFlower on 2017/11/8.
 */
@Service
public class PatternServiceImpl implements IPatternService {

    @Autowired
    private IBaseDao<PatternEntity> patternDao;

    @Override
    public void add(PatternVo patte) {
        PatternEntity entity = new PatternEntity();

        EquipmentEntity equipt=new EquipmentEntity();
        equipt.setId(patte.getEquipmentId());
        entity.setEquipmentEntity(equipt);

        entity.setPatternName(patte.getPatternName());
        entity.setPattern(patte.getPattern());
        patternDao.save(entity);
    }

    @Override
    public void delete(Integer id) {
        Map<String, Object> params = new HashMap<String, Object>();
        params.put("id", id);
        patternDao.executeHql("delete from PatternEntity entity where entity.id=:id",params);
    }

    @Override
    public void edit(PatternVo patte) {
        Map<String, Object> params = new HashMap<String, Object>();

        EquipmentEntity equipment=new EquipmentEntity();
        equipment.setId(patte.getEquipmentId());
        equipment.setEquipmentName(patte.getEquipmentName());
        params.put("equipment", equipment);
        params.put("patternName",patte.getPatternName());
        params.put("pattern",patte.getPattern());
        params.put("id", patte.getId());

        String hql="update PatternEntity entity set entity.equipmentEntity=:equipment," +
                "entity.patternName=:patternName,entity.pattern=:pattern where entity.id=:id";
        patternDao.executeHql(hql, params);
    }

    @Override
    public List<PatternVo> dataGrid(PatternVo patte, PageFilter pageFilter){
        List<PatternVo> ul = new ArrayList<PatternVo>();
        Map<String, Object> params = new HashMap<String, Object>();
        String hql = " from PatternEntity entity ";
        List<PatternEntity> list = patternDao.findList(hql + whereHql(patte, params) + orderHql(pageFilter), params, pageFilter.getPage(), pageFilter.getRows());
        for (PatternEntity entity : list) {
            PatternVo u = new PatternVo();
            BeanUtils.copyProperties(entity, u);

            u.setEquipmentId(entity.getEquipmentEntity().getId());
            u.setEquipmentName(entity.getEquipmentEntity().getEquipmentName());
            ul.add(u);
        }
        return ul;
    }

    @Override
    public Long count(PatternVo patte, PageFilter pageFilter) {
        Map<String, Object> params = new HashMap<String, Object>();
        String hql = " from PatternEntity entity ";
        return patternDao.count("select count(*) " + hql + whereHql(patte, params), params);
    }

    private String whereHql(PatternVo patte, Map<String, Object> params) {
        String hql = "";
        if (patte != null) {
            hql += " where 1=1 ";
        }
        return hql;
    }

    private String orderHql(PageFilter pageFilter) {
        String orderString = "";
        if ((pageFilter.getSort() != null) && (pageFilter.getOrder() != null)) {
            orderString = " order by entity." + pageFilter.getSort() + " " + pageFilter.getOrder();
        }
        return orderString;
    }

    @Override
    public List<Tree> tree() {
        List<PatternEntity> l = null;
        List<Tree> lt = new ArrayList<Tree>();

        l = patternDao.findList("select distinct entity from PatternEntity entity");

        if ((l != null) && (l.size() > 0)) {
            for (PatternEntity r : l) {
                Tree tree = new Tree();
                tree.setId(r.getId().toString());
                lt.add(tree);
            }
        }
        return lt;
    }

    @Override
    public List<PatternEntity> getPatternByEquipment(Integer equipmentId){
        Map<String, Object> params = new HashMap<String, Object>();
        params.put("equipmentId", equipmentId);
        String hql="from PatternEntity c where c.equipmentEntity.id=:equipmentId";
        List<PatternEntity> list=patternDao.findList(hql,params);
        return list;
    }

}
